package Interfaces;

import SistemaCocheras.manejadorCliente;
import SistemaCocheras.manejadorEstadia;
import SistemaCocheras.manejadorVehiculo;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public final class listarClientes extends javax.swing.JFrame {

    private Connection conexion;

    
    public listarClientes() {
        initComponents();
        centrarJFrame();
    }

    public listarClientes(Connection c){
        try {
            initComponents();
            centrarJFrame();
            this.conexion = c;
            manejadorVehiculo mv = new manejadorVehiculo(this.conexion);
            manejadorCliente mc = new manejadorCliente(this.conexion);
            ResultSet listaClientes = mc.buscarClientes();
            String[] columnas = {"DNI", "Nombre", "Apellido", "Direccion", "Telefono", "Tipo de Tarifa", "Tipo Vehiculo" , "Dominios De Vehiculos"};
            int aux = 0;
            String[][] filas = new String[500][11];
            try {
                String patentes = "";
                ResultSet vehiculos = null;
                while( listaClientes.next() ) {
                    ResultSet listaTipoVehiculo = mv.buscarVehiculosPorCliente(listaClientes.getString("dni"));
                    filas[aux][0] = listaClientes.getString("dni");                                 //
                    filas[aux][1] = listaClientes.getString("nombre");                              //
                    filas[aux][2] = listaClientes.getString("apellido");                            // Se llenan las columnas correspondientes del listado
                    filas[aux][3] = listaClientes.getString("direccion");                           //
                    filas[aux][4] = listaClientes.getString("telefono");                            //
                    filas[aux][5] = tipoDeTarifa(listaClientes.getString("tipotarifa"));            //
                    //-----------------------------------------------------------------------------------
                    if( listaTipoVehiculo.next() ){                                                 //
                        filas[aux][6] = tipoDeMovil(listaTipoVehiculo.getObject("tipo"));           // Se llena la columna "Tipo Vehiculo" del listado
                    }                                                                               //
                    // -----------------------------------------------------------------------------------
                    patentes = "#";                                                                     //
                    vehiculos = mv.buscarVehiculosPorCliente(listaClientes.getString("dni"));  //
                    while (vehiculos.next()){                                                           // Calculo de patentes asociadas a un cliente
                        patentes += vehiculos.getString("dominio")+"#";                                 //
                    }                                                                                   //
                    filas[aux][7]= patentes;                                                            //
                    aux++;
                    // -----------------------------------------------------------------------------------
                }
                DefaultTableModel tabla = new DefaultTableModel(filas, columnas);
                tabla.setDataVector(filas, columnas);
                tablaListado.setModel(tabla);
            } catch (SQLException ex) {
                Logger.getLogger(listarClientes.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listarClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }// Fin constructor listarClientes


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonCerrarListado = new javax.swing.JButton();
        botonMostrarListado = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaListado = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("B&B - Listado de clientes");
        setResizable(false);

        botonCerrarListado.setText("Cerrar listado");
        botonCerrarListado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarListadoActionPerformed(evt);
            }
        });

        botonMostrarListado.setText("Mostrar listado");
        botonMostrarListado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonMostrarListadoActionPerformed(evt);
            }
        });

        tablaListado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "DNI", "Nombre", "Apellido", "Direccion", "Telefono", "Tipo de Tarifa", "Tipo Vehiculo", "Dominios De Vehiculos"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaListado);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1062, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(299, 299, 299)
                .addComponent(botonMostrarListado, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 246, Short.MAX_VALUE)
                .addComponent(botonCerrarListado, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(302, 302, 302))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonCerrarListado, botonMostrarListado});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonMostrarListado)
                    .addComponent(botonCerrarListado, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonCerrarListado, botonMostrarListado});

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void botonMostrarListadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonMostrarListadoActionPerformed
        try {
            initComponents();
            centrarJFrame();
            manejadorVehiculo mv = new manejadorVehiculo(this.conexion);
            manejadorCliente mc = new manejadorCliente(this.conexion);
            ResultSet listaClientes = mc.buscarClientes();
            String[] columnas = {"DNI", "Nombre", "Apellido", "Direccion", "Telefono", "Tipo de Tarifa", "Tipo Vehiculo" , "Dominios De Vehiculos"};
            int aux = 0;
            String[][] filas = new String[500][11];
            try {
                String patentes = "";
                ResultSet vehiculos = null;
                while( listaClientes.next() ) {
                    ResultSet listaTipoVehiculo = mv.buscarVehiculosPorCliente(listaClientes.getString("dni"));
                    filas[aux][0] = listaClientes.getString("dni");                                 //
                    filas[aux][1] = listaClientes.getString("nombre");                              //
                    filas[aux][2] = listaClientes.getString("apellido");                            // Se llenan las columnas correspondientes del listado
                    filas[aux][3] = listaClientes.getString("direccion");                           //
                    filas[aux][4] = listaClientes.getString("telefono");                            //
                    filas[aux][5] = tipoDeTarifa(listaClientes.getString("tipotarifa"));            //
                    //-----------------------------------------------------------------------------------
                    if( listaTipoVehiculo.next() ){                                                 //
                        filas[aux][6] = tipoDeMovil(listaTipoVehiculo.getObject("tipo"));           // Se llena la columna "Tipo Vehiculo" del listado
                    }                                                                               //
                    // -----------------------------------------------------------------------------------
                    patentes = "#";                                                                     //
                    vehiculos = mv.buscarVehiculosPorCliente(listaClientes.getString("dni"));  //
                    while (vehiculos.next()){                                                           // Calculo de patentes asociadas a un cliente
                        patentes += vehiculos.getString("dominio")+"#";                                 //
                    }                                                                                   //
                    filas[aux][7]= patentes;                                                            //
                    aux++;                                                                                  
                    // -----------------------------------------------------------------------------------
                }
                DefaultTableModel tabla = new DefaultTableModel(filas, columnas);
                tabla.setDataVector(filas, columnas);
                tablaListado.setModel(tabla);
            } catch (SQLException ex) {
                Logger.getLogger(listarClientes.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listarClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_botonMostrarListadoActionPerformed

// ==============================================================================================
// =========================     METODOS AUXILIARES     =========================================
// ==============================================================================================
    public String tipoDeMovil (Object tipoAuto){
        String tipo = "";
        if(tipoAuto.toString().compareTo("0") == 0){
            tipo = "Automovil";
        }
        else{
            if(tipoAuto.toString().compareTo("1") == 0){
                tipo = "Camioneta";
            }
            else{
                if(tipoAuto.toString().compareTo("2") == 0){
                    tipo = "Motocicleta";
                }
                else{
                    if(tipoAuto.toString().compareTo("3") == 0){
                        tipo = "Bicicleta";
                    }
                    else{
                        tipo = "Utilitario";
                    }
                }
            }
        }
        return tipo;
    }

//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
    
    public String tipoDeTarifa (Object tipoTarifa){
        String tipoT = "";
        if (tipoTarifa.toString().compareTo("0") == 0){
            tipoT = "Semanal";
        }
        else{
            if( tipoTarifa.toString().compareTo("1") == 0 ){
                tipoT = "Quincenal";
            }
            else{
                tipoT = "Mensual";
            }
        }
        return tipoT;
    }


// ==============================================================================================
// ==============================================================================================
// ==============================================================================================
    
    
    private void botonCerrarListadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarListadoActionPerformed
        listarClientes.this.dispose();
    }//GEN-LAST:event_botonCerrarListadoActionPerformed


    private void centrarJFrame(){
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frame =this.getSize();
        this.setLocation((pantalla.width-frame.width)/2, (pantalla.height-frame.height)/2);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new listarClientes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCerrarListado;
    private javax.swing.JButton botonMostrarListado;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaListado;
    // End of variables declaration//GEN-END:variables

    private boolean esNoMoroso(ResultSet ultimoPeriodoCliente) throws SQLException {
        manejadorEstadia me = new manejadorEstadia(this.conexion);
        String [] parsing = me.calcularFecha().split("-");
        int anioActual = Integer.parseInt(parsing[0]);
        int mesActual = Integer.parseInt(parsing[1]);
        int diaActual = Integer.parseInt(parsing[2]);
        if (ultimoPeriodoCliente.next()){
            String [] parsing2 = (ultimoPeriodoCliente.getString("fechafin")).split("-");
            int anioPer = Integer.parseInt(parsing2[0]);
            int mesPer = Integer.parseInt(parsing2[1]);
            int diaPer = Integer.parseInt(parsing2[2]);
            return ((anioActual<anioPer)||((anioActual==anioPer)&&(mesActual<mesPer))||((anioActual==anioPer)&&(mesActual==mesPer)&&(diaActual<=diaPer)));
        }
        else{
            return false;
        }
    }

}
